package com.cmccgz.udacs_landlord_webapp.main.warning.view;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.baselibrary.utils.InputMethodUtil;
import com.cmccgz.baselibrary.view.ClearEditText;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.WarningListAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.WarningItem;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.main.warning.WarningActivity;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningListContract;
import com.cmccgz.udacs_landlord_webapp.main.warning.presenters.WarningListPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.cmccgz.udacs_landlord_webapp.utils.YRecyclerView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import static com.cmccgz.baselibrary.utils.ToastUtils.toastShort;

/**
 * 报障列表
 * @author INX
 */
public class WarningListFragment extends BaseFragment implements WarningListContract.View, XRecyclerView.LoadingListener,View.OnClickListener, OnRvItemClickListenter<WarningItem>,ClearEditText.OnTextChangeListener {

    private WarningListContract.Presenter presenter;
    private XRecyclerView rv_warning;
    private View layout_search;
    private ClearEditText et_search;
    private TextView btn_search;
    private DeviceInfoBean deviceInfoBean;
    private WarningActivity parentActivity;
    private boolean isAttendant;//是否维护人员
    private WarningListAdapter adapter;
    private List<WarningItem> datas=new ArrayList<>();
    private int page=1;
    private LinearLayoutManager layoutManager;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_warning_list;
    }

    @Override
    protected void initView() {
        parentActivity = ((WarningActivity) getActivity());
        ViewHelper viewHelper = ViewHelper.instance(this);
        viewHelper.setTitle("报障列表");
        viewHelper.setImageRight(R.drawable.add_white);
        viewHelper.setImageRightVisable(true);
        viewHelper.setBackTextVisable(true);
        viewHelper.setTitleOnClicklistener(this);
        rv_warning = ((YRecyclerView) findViewById(R.id.rv_warning));
        layout_search = findViewById(R.id.layout_search);
        et_search = ((ClearEditText) findViewById(R.id.et_search));
        btn_search = ((TextView) findViewById(R.id.btn_search));
        presenter = new WarningListPresenter(this);
        adapter = new WarningListAdapter(datas,mContext);
        adapter.setListenter(this);
        layoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        rv_warning.setLayoutManager(layoutManager);
        rv_warning.setLoadingListener(this);
        rv_warning.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_warning.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_warning.setAdapter(adapter);
        rv_warning.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        btn_search.setOnClickListener(this);
        initDatas();
        initList();
        initInputMethod();
    }
    private void initInputMethod() {
        et_search.setOnTextChangeListener(this);
        InputMethodUtil inputMethodUtil = new InputMethodUtil(mContext);
        inputMethodUtil.setupUI(root, et_search);
        inputMethodUtil.setSearchBtn(et_search,btn_search);
    }
    public void initList() {
        showLoadingDialog("正在加载数据");
        onRefresh();
    }

    private void initDatas() {
        isAttendant =parentActivity.isAttendant();
        Bundle arguments = getArguments();
        if (arguments==null) {
            return;
        }
        deviceInfoBean = ((DeviceInfoBean) arguments.getSerializable("device"));
    }

    @Override
    public void refreshSuccess(List<WarningItem> datas) {
        this.datas.clear();
        if (datas!=null) {
            this.datas.addAll(datas);
        }
        adapter.notifyDataSetChanged();
        layoutManager.scrollToPosition(0);
        stopAll();
    }

    @Override
    public void loadMoreSuccess(List<WarningItem> datas) {
        if (datas!=null) {
            this.datas.addAll(datas);
        }
        adapter.notifyDataSetChanged();
        stopAll();
    }

    @Override
    public void refreshDatasError(String msg) {
        page=1;
        toastShort(mContext,msg);
        stopAll();
    }

    @Override
    public void loadDatasError(String msg) {
        page--;
        page=page<1?1:page;
        toastShort(mContext,msg);
        stopAll();
    }

    @Override
    public void onRefresh() {
        page=1;
        presenter.getDatas(et_search.getText().toString(),"","","1");
    }

    @Override
    public void onLoadMore() {
        page++;
        presenter.getDatas(et_search.getText().toString(),"","",page+"");
    }

    public void stopAll() {
        dismissLoadingDialog();
        rv_warning.refreshComplete();
        rv_warning.loadMoreComplete();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                parentActivity.onBackPressed();
                break;
            case R.id.btn_search:
                initList();
                break;
            case R.id.iv_right:
//                parentActivity.editWarning(false);
                parentActivity.selectDevices();
                break;
        }
    }

    @Override
    public void onItemClick(WarningItem warningItem, int position, View itemView) {
        if (warningItem!=null) {
            parentActivity.showWarningDetail(warningItem.w_id);
        }
    }

    @Override
    public void onTextChange(String text) {

    }

    @Override
    public void onClearText() {
        initList();
    }
}
